package com.urtime.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.urtime.dao.EventDAO;
import com.urtime.model.Event;
import com.urtime.util.ErrorJson;
import com.urtime.util.HibernateUtil;
import com.urtime.util.StringUtil;

/**
 * Servlet implementation class GetDispatcher
 */
public class GetDispatcher extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetDispatcher() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if(method==null||method.length()==0){
			ServletUtil.printError(request,response,ErrorJson.ERROR_UNKNOWN_METHOD, null);
			return;
		}
		if("gettags".equals(method)){
			doGetTags(request,response);
		}else if("myevents".equals(method)){
			doMyEvents(request,response);
		}else{
			ServletUtil.printError(request,response,ErrorJson.ERROR_UNKNOWN_METHOD, method);
		}
	}

	private void doMyEvents(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		if(!ServletUtil.checkAccess(request, response))
			return;
		//response.getWriter().println("TODO...");
		HttpSession session=request.getSession(false);
		int userid = (Integer) session.getAttribute("userid");
		List<Event> list = EventDAO.queryEventByUser(userid, "");
		JSONArray resp = new JSONArray();
		for(Event e:list){
			resp.add(EventDAO.eventToJson(e));
		}
		response.getWriter().print(resp);
	}

	private void doGetTags(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String tag = request.getParameter("tag");
		String p = request.getParameter("page");
		int page = 0;
		JSONArray list = new JSONArray();
		response.setContentType("text/plain");
		String cond;
		if(tag==null||tag.isEmpty()){ //return all
			 cond = ""; 
		}else{
			tag = StringUtil.stringFilter(tag);
			cond = "where t.name like '%"+tag+"%'";
		}
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Object[]> l = (List<Object[]>) session.createQuery
				("SELECT t.id,t.name FROM Tag t "+cond).list();
		for(Object[] obj:l){
			JSONObject item = new JSONObject();
			item.put(obj[0], obj[1]);
			list.add(item);
		}
		session.getTransaction().commit();
		response.getWriter().print(list);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletUtil.printError(request, response, ErrorJson.ERROR_UNKNOWN_METHOD, "POST");
	}

}
